package com.example.front.main.community;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.front.MyApplication;
import com.example.front.R;
import com.example.front.main.MainActivity;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

/**
 * 房间介绍界面类
 */
public class RoomIntroductionActivity extends AppCompatActivity {
    private String URL;
    private MyApplication myApplication;
    private TextView name_textView;
    private TextView type_textView;
    private TextView price_textView;
    private TextView count_textView;
    private TextView maxCount_textView;
    private TextView remark_textView;
    private Button join_button;
    private String name = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room_introduction);
        name_textView = findViewById(R.id.name_textView);
        type_textView = findViewById(R.id.type_textView);
        price_textView = findViewById(R.id.price_textView);
        count_textView = findViewById(R.id.count_textView);
        maxCount_textView = findViewById(R.id.maxCount_textView);
        remark_textView = findViewById(R.id.remark_textView);
        join_button = findViewById(R.id.join_button);
        myApplication = (MyApplication)getApplication();
        URL = myApplication.getURL();
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        name = bundle.getString("name", "");
        String type = bundle.getString("type", "");
        double cost = bundle.getDouble("cost");
        int count = bundle.getInt("count", 0);
        int maxCount = bundle.getInt("maxCount", 0);
        int state = bundle.getInt("state", 0);
        String remark = bundle.getString("remark", "");
        if(count >= maxCount || state == 0)join_button.setVisibility(View.INVISIBLE);
        name_textView.setText(name + "房");
        type_textView.setText(type + "");
        price_textView.setText(cost + "元/时");
        count_textView.setText("" + count);
        maxCount_textView.setText("" + maxCount);
        if(remark.equals("null"))remark_textView.setText("  暂无介绍");
        else remark_textView.setText("  " + remark);
    }

    public void join_room_onClick(View view){
        SharedPreferences sharedPreferences = getSharedPreferences("Login", Context.MODE_PRIVATE);
        String account = sharedPreferences.getString("account", "");
        OkHttpUtils
                .post()
                .url(URL + "/room/joinRoom")
                .addParams("account", account)
                .addParams("name", name)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                    }
                    @Override
                    public void onResponse(String response) {
                        if(response.equals("true")) {
                            Toast.makeText(RoomIntroductionActivity.this, "加入成功！", Toast.LENGTH_SHORT).show();
                            finish();
                        }
                        else
                            Toast.makeText(RoomIntroductionActivity.this, "加入失败！", Toast.LENGTH_SHORT).show();
                    }
                });
    }
}
